package com.yuehuanghun.admin.core.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.UserConstants;
import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.core.domain.SysPost;
import com.yuehuanghun.admin.core.service.ISysPostService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.exception.ValidateException;
import com.yuehuanghun.framework.web.BaseResource;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 岗位信息操作处理
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/post")
@SaCheckLogin
public class SysPostController extends BaseResource {
	@Autowired
	private ISysPostService postService;

	/**
	 * 获取岗位列表
	 */
	@GetMapping("/list")
	@SaCheckPermission("system:post:list")
	public PageDTO<SysPost> list(SysPost post) { //NOSONAR
		List<SysPost> list = postService.getByExample(post, getPage());
		return toPage(list);
	}

	@Log(title = "岗位管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("system:post:export")
	public String export(SysPost post) { //NOSONAR
		List<SysPost> list = postService.getByExample(post);
		ExcelUtil<SysPost> util = new ExcelUtil<>(SysPost.class);
		return util.exportExcel(list, "岗位数据");
	}

	/**
	 * 根据岗位编号获取详细信息
	 */
	@GetMapping(value = "/{postId}")
	@SaCheckPermission("system:post:query")
	public SysPost getInfo(@PathVariable Long postId) {
		return postService.getById(postId).orElse(null);
	}

	/**
	 * 新增岗位
	 */
	@Log(title = "岗位管理", businessType = BusinessType.INSERT)
	@PostMapping
	@SaCheckPermission("system:post:add")
	public void add(@Validated @RequestBody SysPost post) {
		if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
			throw new ValidateException("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
			throw new ValidateException("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
		}
		postService.add(post);
	}

	/**
	 * 修改岗位
	 */
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("system:post:edit")
	public void edit(@Validated @RequestBody SysPost post) {
		if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
			throw new ValidateException("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
			throw new ValidateException("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
		}
		postService.updateById(post);
	}

	/**
	 * 删除岗位
	 */
	@Log(title = "岗位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
	@SaCheckPermission("system:post:remove")
	public void remove(@PathVariable List<Long> postIds) {
		postService.deleteByIds(postIds);
	}

	/**
	 * 获取岗位选择框列表
	 */
	@GetMapping("/optionselect")
	public List<SysPost> optionselect() {
		return postService.getAll();
	}
}
